package top.littleze.aiattendance.service.impl;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.littleze.aiattendance.dao.AttendanceCourseEntityMapper;
import top.littleze.aiattendance.dao.AttendanceLogEntityMapper;
import top.littleze.aiattendance.dao.AttendanceSCEntityMapper;
import top.littleze.aiattendance.dao.AttendanceTCEntityMapper;
import top.littleze.aiattendance.entity.*;
import top.littleze.aiattendance.service.TeacherService;
import top.littleze.aiattendance.utils.page.PageResult;
import top.littleze.aiattendance.utils.page.Reply;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author ZhaoGengze
 * @Date 2021/7/9 11:39
 */

@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    AttendanceTCEntityMapper attendanceTCEntityMapper;
    @Autowired
    AttendanceSCEntityMapper attendanceSCEntityMapper;
    @Autowired
    AttendanceLogEntityMapper logEntityMapper;
    @Autowired
    AttendanceCourseEntityMapper courseEntityMapper;

    /**
     * 授课列表查询
     * @param teacherId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Reply<List<AttendanceCourseEntity>> queryTeacherCourse(String teacherId, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<AttendanceCourseEntity> attendanceCourseEntityList = attendanceTCEntityMapper.queryTeacherCourse(teacherId);
        System.out.println(attendanceCourseEntityList);
        return Reply.success(attendanceCourseEntityList, attendanceCourseEntityList.size());
    }

    /**
     * 课程学生名单查询
     * @param courseId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Reply<List<AttendanceStudentEntity>> queryStudentList(String courseId, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<AttendanceStudentEntity> listAttendanceStudentEntities = attendanceSCEntityMapper.queryStudentList(courseId);
        return Reply.success(listAttendanceStudentEntities, listAttendanceStudentEntities.size());
    }

    /**
     * 删除课程
     * @param courseId
     * @return
     */
    @Override
    @Transactional
    public Reply<Void> deleteCourse(String courseId) {
        // 查询是否存在签到记录
        Boolean flag = logEntityMapper.queryExistAttendanceLog(courseId);
        log.info("是否存在签到记录：{}", flag);
        // 存在提示
        if (flag) {
            return Reply.failuer(1, "该课程存在签到记录");
        }
        // 不存在可以删除
        else {
            // 删除Course表，逻辑删除
            courseEntityMapper.deleteByCourseId(courseId);
            // 删除TC表，逻辑删除
            attendanceTCEntityMapper.deleteByCourseId(courseId);
            // 删除SC表，逻辑删除
            attendanceSCEntityMapper.deleteByCourseID(courseId);
        }
        return Reply.success(0, "删除成功！");
    }

    /**
     * 根据id修改课程信息
     * @param courseEntity
     * @return
     */
    @Override
    public Reply<Void> updateCourse(AttendanceCourseEntity courseEntity) {
        // 根据id修改，前端需要传相应的值
        int nums = courseEntityMapper.updateCourse(courseEntity);
        if (nums == 1) {
            return Reply.success(0, "更新成功！");
        } else {
            return Reply.failuer(1, "更新失败，请稍后再试");
        }
    }

    /**
     * 新建课程
     * @param courseEntity
     * @return
     */
    @Override
    public Reply<Void> insertCourse(AttendanceCourseEntity courseEntity) {
        int nums = courseEntityMapper.insertSelective(courseEntity);
        if (nums == 1) {
            return Reply.success();
        } else {
            return Reply.failuer(1, "更新失败，请稍后再试");
        }
    }

    /**
     * 出勤查询
     * @param courseId
     * @return
     */
    @Override
    public Reply<List<AttendanceLogEntity>> queryAttendance(String courseId) {
        List<AttendanceLogEntity> logEntityList = logEntityMapper.queryAttendance(courseId);
        if (logEntityList != null && logEntityList.size() > 0) {
            // 统计已签到人数
            int attendanceStudentNumber = (int) logEntityList.stream().filter(o -> o.getCheckin() == true).count();
            // 统计迟到人数
            int lateStudentNumber = (int) logEntityList.stream().filter(o -> o.getCheckin() == true && o.getLate() == true).count();
            // TODO 返回值视情况修改，其余统计数据视情况添加
            log.info("已签到人数：{}，迟到人数：{}", attendanceStudentNumber, lateStudentNumber);
        }
        return Reply.success(logEntityList, logEntityList.size());
    }

    @Override
    public Reply<List<AttendanceSCEntity>> queryCourseStudentList(String teacherId, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<AttendanceSCEntity> scEntityList = attendanceSCEntityMapper.queryCourseStudentList(teacherId);
        System.out.println(scEntityList);
        return Reply.success(scEntityList, scEntityList.size());
    }


}


